import http from './interface'
import md5 from './md5.js'
/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 * 
 */

// 单独导出(测试接口) import {test} from '@/common/vmeitime-http/'
export const test = (url,data,) => {
	http.config.baseUrl = "https://task.nuofengtc.com"
	//设置请求前拦截器
	http.interceptor.request = (config) => {
		var sortString = sortParams(data);
		var ascii = Str2Ascii(sortString);
		var sign = md5(ascii).toUpperCase().substring(0,18)
		config.header = {
			"sign": sign
		}
	}
	//设置请求结束后拦截器
	http.interceptor.response = (response) => {
		console.log('个性化response....')
		//判断返回状态 执行相应操作
		return response;
	}
    return http.request({
		baseUrl: http.config.baseUrl,
        url: url,
		dataType: 'json',
        data,
    })
}

function sortParams(data){
		// console.log("参数："+JSON.stringify(data))
	let keys = Object.keys(data).sort();
	  let newParams = {};
	  var i = 0;
	  var s = ''
	  keys.forEach((key) => {
		
			  s = s+  key+"="+data[key]+"&"
		  
	   
	  });
	 s = s+"appkey=ruixue";
	 return s;
}
function Str2Ascii(str){
	var ascii = '';
	for(var i = 0;i<str.length;i++){
		ascii = ascii+str[i].charCodeAt();
	}
	return ascii;
}


// 轮播图
export const banner = (data) => {
    return http.request({
        url: '/banner/36kr',
        method: 'GET', 
        data,
		// handle:true
    })
}

// 默认全部导出  import api from '@/common/vmeitime-http/'
export default {
	test,
    banner
}